package com.dm.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dm.shop.entity.*;
import com.dm.shop.service.CommentService;
import com.dm.shop.utils.CommonUtil;
import com.dm.shop.utils.ResultUtil;
import com.dm.shop.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MyBatisPlusGenerater-Saiya Wang
 * @since 2021-07-20
 */
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping("/add")
    @ApiOperation("添加评论")
    public Result addComment(@RequestBody Comment comment)
    {
        User user=CommonUtil.getCurrentUser();//获取当前的用户id
        comment.setUid(user.getId());
        return commentService.addComment(comment);

    }

    @PostMapping("/total")
    @ApiOperation("获取评论总数量")
    public Result getTotalComment()
    {
        return ResultUtil.success(commentService.list(null).size());
    }


    @GetMapping("/{pid}")
    @ApiOperation("获取一个商品的所有评论")
    public Result getOneComment(@PathVariable("pid") Integer pid)
    {
        //条件构造器
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",pid);
        List<Comment> commentList = commentService.list(wrapper);
        return ResultUtil.success("查找成功",commentList);
    }

}
